/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.ui;

import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;

/**
 * Handles plugin images.
 */
public class ZGaeUIImages
{
    /** Singleton instance. */
    private static final ZGaeUIImages sInstance = new ZGaeUIImages();

    /**
     * Singleton getter.
     */
    public static final ZGaeUIImages getInstance()
    {
        return sInstance;
    }

    /**
     * Gets an image at the given path.
     *
     * @param aImagePath
     */
    public static Image getImage(String aImagePath)
    {
        return sInstance.getImageInternal(aImagePath);
    }

    /**
     * Gets an image descriptor for the given path.
     *
     * @param aImagePath
     */
    public static ImageDescriptor getImageDescriptor(String aImagePath)
    {
        return sInstance.getImageDescriptorInternal(aImagePath);
    }

    /** Cache of images. */
    private Map<String, Image> mImageCache = new HashMap<String, Image>();

    /**
     * Private singleton c'tor.
     */
    private ZGaeUIImages()
    {
    }

    /**
     * Gets an image at the given path.
     *
     * @param aImagePath
     */
    private synchronized Image getImageInternal(String aImagePath)
    {
        Image image = getImageCache().get(aImagePath);
        if (image == null)
            image = loadImageInternal(aImagePath);
        return image;
    }

    /**
     * Loads an image and caches it.
     *
     * @param aImagePath
     */
    private Image loadImageInternal(String aImagePath)
    {
        Image image = getImageDescriptorInternal(aImagePath).createImage();
        getImageCache().put(aImagePath, image);
        return image;
    }

    /**
     * Gets an image descriptor.
     *
     * @param aImagePath
     */
    private ImageDescriptor getImageDescriptorInternal(String aImagePath)
    {
        URL imageEntry = ZGaeUIActivator.getDefault().getBundle().getEntry(aImagePath);
        return ImageDescriptor.createFromURL(imageEntry);
    }

    /**
     * Dispose of all the images in the cache.
     */
    public synchronized void dispose()
    {
        Collection<Image> images = getImageCache().values();
        for (Image image : images)
        {
            image.dispose();
        }
        getImageCache().clear();
    }

    /**
     * @return the imageCache
     */
    protected Map<String, Image> getImageCache()
    {
        return mImageCache;
    }

    /**
     * @param aImageCache the imageCache to set
     */
    protected void setImageCache(Map<String, Image> aImageCache)
    {
        mImageCache = aImageCache;
    }
}
